package com.gszkc.service.impl;

import com.gszkc.context.BaseContext;
import com.gszkc.dto.ShoppingCartDTO;
import com.gszkc.entity.Dish;
import com.gszkc.entity.Setmeal;
import com.gszkc.entity.ShoppingCart;
import com.gszkc.mapper.DishMapper;
import com.gszkc.mapper.SetmealMapper;
import com.gszkc.mapper.ShoppingCartMapper;
import com.gszkc.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addshoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前商品是否存在
        //select * from shopping_cart where user_id = ? and setmeal_id = ?;
        //select * from shopping_cart where user_id = ? and dish_id = ? and dish_flavor = ?;
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //存在了则数量加一即可
        if (list != null && list.size() > 0){
            ShoppingCart shoppingCartTempData = list.get(0);
            shoppingCartTempData.setNumber(shoppingCartTempData.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCartTempData);
        }else {//不存在则插入一条新的商品数
            //先判断是商品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null){
                //如果是菜品则查询原数据然后添加并插入
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShopping() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteById(userId);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void cleanOneShopping(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果购物车不为空
        if(list != null && list.size() > 0){
            //只可能查到一个用户数据,所以直接取列表第1(0)位即可
            shoppingCart = list.get(0);
            //获取数量
            Integer number = shoppingCart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
